<html>
<head><meta charset="utf-8"><title>&#x27;higher ranked subtype error&#x27; · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/&#x27;higher.20ranked.20subtype.20error&#x27;.html">&#x27;higher ranked subtype error&#x27;</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="219771785"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/%27higher%20ranked%20subtype%20error%27/near/219771785" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/&#x27;higher.20ranked.20subtype.20error&#x27;.html#219771785">(Dec 13 2020 at 17:53)</a>:</h4>
<p>I've run into this error a couple times in the last week and I'm trying to figure it out. The first time was <a href="https://github.com/raphlinus/pulldown-cmark/issues/509">https://github.com/raphlinus/pulldown-cmark/issues/509</a>, and I figured out why my code didn't work, but not what the error was trying to tell me. The second time is just now, I made the following change to rustdoc:</p>
<div class="codehilite" data-code-language="Diff"><pre><span></span><code><span class="gh">diff --git a/src/librustdoc/lib.rs b/src/librustdoc/lib.rs</span>
<span class="gh">index 286a29edd95..60aa6485c7a 100644</span>
<span class="gd">--- a/src/librustdoc/lib.rs</span>
<span class="gi">+++ b/src/librustdoc/lib.rs</span>
<span class="gu">@@ -117,21 +117,9 @@ fn get_args() -&gt; Option&lt;Vec&lt;String&gt;&gt; {</span>
         .collect()
 }

<span class="gd">-fn stable&lt;F&gt;(name: &amp;'static str, f: F) -&gt; RustcOptGroup</span>
<span class="gd">-where</span>
<span class="gd">-    F: Fn(&amp;mut getopts::Options) -&gt; &amp;mut getopts::Options + 'static,</span>
<span class="gd">-{</span>
<span class="gd">-    RustcOptGroup::stable(name, f)</span>
<span class="gd">-}</span>
<span class="gd">-</span>
<span class="gd">-fn unstable&lt;F&gt;(name: &amp;'static str, f: F) -&gt; RustcOptGroup</span>
<span class="gd">-where</span>
<span class="gd">-    F: Fn(&amp;mut getopts::Options) -&gt; &amp;mut getopts::Options + 'static,</span>
<span class="gd">-{</span>
<span class="gd">-    RustcOptGroup::unstable(name, f)</span>
<span class="gd">-}</span>
<span class="gd">-</span>
 fn opts() -&gt; Vec&lt;RustcOptGroup&gt; {
<span class="gi">+    let stable: fn(_, fn(&amp;mut getopts::Options) -&gt; _) -&gt; _ = RustcOptGroup::stable;</span>
<span class="gi">+    let unstable: fn(_, fn(&amp;mut getopts::Options) -&gt; _) -&gt; _ = RustcOptGroup::unstable;</span>
     vec![
         stable("h", |o| o.optflag("h", "help", "show this help message")),
         stable("V", |o| o.optflag("V", "version", "print rustdoc's version")),
</code></pre></div>
<p>(I can't just <code>use</code> the functions because they're associated on a type, booo).</p>
<p>That gives errors like this:</p>
<div class="codehilite"><pre><span></span><code>error: lifetime may not live long enough
   --&gt; src/librustdoc/lib.rs:436:31
    |
436 |         unstable(&quot;check&quot;, |o| o.optflag(&quot;&quot;, &quot;check&quot;, &quot;Run rustdoc checks&quot;)),
    |                            -- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ returning this value requires that `&#39;1` must outlive `&#39;2`
    |                            ||
    |                            |return type of closure is &amp;&#39;2 mut rustc_session::getopts::Options
    |                            has type `&amp;&#39;1 mut rustc_session::getopts::Options`
error: higher-ranked subtype error
   --&gt; src/librustdoc/lib.rs:139:64
    |
139 |     let unstable: fn(_, fn(&amp;mut getopts::Options) -&gt; _) -&gt; _ = RustcOptGroup::unstable;
    |                                                                ^^^^^^^^^^^^^^^^^^^^^^^
</code></pre></div>
<p>(plus another 64 times for each <code>unstable</code> call)</p>
<p>What does this actually mean? Clearly it's not wrong code because it worked before. Is this a borrow checker limitation?</p>
<p>For comparison, here's the error if I don't specify the type of the <code>let</code>:</p>
<div class="codehilite"><pre><span></span><code>error[E0282]: type annotations needed
   --&gt; src/librustdoc/lib.rs:124:22
    |
124 |         stable(&quot;h&quot;, |o| o.optflag(&quot;h&quot;, &quot;help&quot;, &quot;show this help message&quot;)),
    |                      ^ consider giving this closure parameter a type
    |
    = note: type must be known at this point
</code></pre></div>



<a name="219776502"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/%27higher%20ranked%20subtype%20error%27/near/219776502" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/&#x27;higher.20ranked.20subtype.20error&#x27;.html#219776502">(Dec 13 2020 at 19:30)</a>:</h4>
<p>I think the lifetime desugaring for <code>fn(&amp;mut getopts::Options) -&gt; _</code> is different than with the freestanding functions</p>



<a name="219776542"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/%27higher%20ranked%20subtype%20error%27/near/219776542" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/&#x27;higher.20ranked.20subtype.20error&#x27;.html#219776542">(Dec 13 2020 at 19:31)</a>:</h4>
<p>with the freestanding functions, the argument and return type of the <code>F</code> function get desugared to the same lifetime</p>



<a name="219776557"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/%27higher%20ranked%20subtype%20error%27/near/219776557" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/&#x27;higher.20ranked.20subtype.20error&#x27;.html#219776557">(Dec 13 2020 at 19:31)</a>:</h4>
<p>e.g. <code>F: for&lt;'a&gt; Fn(&amp;'a mut _) -&gt; &amp;'a mut _</code></p>



<a name="219776621"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/%27higher%20ranked%20subtype%20error%27/near/219776621" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/&#x27;higher.20ranked.20subtype.20error&#x27;.html#219776621">(Dec 13 2020 at 19:32)</a>:</h4>
<p>with the local variable function pointer, you're left the return type of the <code>fn(&amp;mut getopts::Options)</code> to be inferred</p>



<a name="219776651"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/%27higher%20ranked%20subtype%20error%27/near/219776651" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/&#x27;higher.20ranked.20subtype.20error&#x27;.html#219776651">(Dec 13 2020 at 19:33)</a>:</h4>
<p>which means that it won't get desugared to the higher-ranked lifetime used with the freestanding functions</p>



<a name="219776721"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/%27higher%20ranked%20subtype%20error%27/near/219776721" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/&#x27;higher.20ranked.20subtype.20error&#x27;.html#219776721">(Dec 13 2020 at 19:34)</a>:</h4>
<p><span class="user-mention" data-user-id="232545">@Joshua Nelson</span> What happens if you change the return type to <code>&amp;mut _</code></p>



<a name="219777830"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/%27higher%20ranked%20subtype%20error%27/near/219777830" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lqd <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/&#x27;higher.20ranked.20subtype.20error&#x27;.html#219777830">(Dec 13 2020 at 19:54)</a>:</h4>
<p>(matthew is working on turning those inscrutable errors back to what they were prior to the universes and leak-check changes)</p>



<a name="219778767"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/%27higher%20ranked%20subtype%20error%27/near/219778767" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/&#x27;higher.20ranked.20subtype.20error&#x27;.html#219778767">(Dec 13 2020 at 20:12)</a>:</h4>
<p><span class="user-mention silent" data-user-id="125294">Aaron Hill</span> <a href="#narrow/stream/122651-general/topic/'higher.20ranked.20subtype.20error'/near/219776721">said</a>:</p>
<blockquote>
<p><span class="user-mention silent" data-user-id="232545">Joshua Nelson</span> What happens if you change the return type to <code>&amp;mut _</code></p>
</blockquote>
<p>omg that fixed it</p>



<a name="219778769"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/%27higher%20ranked%20subtype%20error%27/near/219778769" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/&#x27;higher.20ranked.20subtype.20error&#x27;.html#219778769">(Dec 13 2020 at 20:12)</a>:</h4>
<p>ahhhh</p>



<a name="219778771"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/%27higher%20ranked%20subtype%20error%27/near/219778771" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/&#x27;higher.20ranked.20subtype.20error&#x27;.html#219778771">(Dec 13 2020 at 20:12)</a>:</h4>
<p>thank you so much!</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>